#include "cross.h"

void cross_seed(double bestseed[3][6],double sndseed[3][6])
{
	/*
	    SecondBestSeed : [a,2,b,1,c,3,d,5]
	             |            _ _
	            \/
	          BestSeed : [1,2,3,4,5,6,7,8]               
	               ||         _ _
	              ||
	           NewSeed : [1,2,b,1,5,6,7,8]
	                          _ _   
	*/ 
    int i,j;

    for(j=0;j<3;j++){
        int GenePoint  = randint(0,5);
         /* if Point is 3,length is 3
          * if Point is 0,length is 6
          * if Point is 2,length is 4
          */
        int LengthLimit = 6-GenePoint;
        int GeneLength = (LengthLimit>=3)?randint(1,3):LengthLimit;
        printf("Point:%d\tLength:%d\n",GenePoint,GeneLength);
        for(i=0;i<=GeneLength;i++){
            bestseed[j][GenePoint+i] = sndseed[j][GenePoint+i];
        }
    }
}



/*
 *  return new Population
 */
void _cross_population(plt * NewPopulation,plt * TestPopulation){
	cross_seed(NewPopulation->parement,TestPopulation->parement);
	NewPopulation->result = all_cost(NewPopulation->parement);
}




plt * cross_best(plt * Top5Population[5]){            
	/*
		   plt1 cross -> [plt2,plt3,plt4,plt5]
		   every loop  filter better cross-result
           return the Best
	*/      
	plt * BestPopulation = init_plt();
	memcpy(BestPopulation,Top5Population[0],sizeof(plt));	
	                                                        
	int i;
	for(i=1;i<5;i++)
	{                                                    
		_cross_population(BestPopulation,Top5Population[i]);
		
		if ( BestPopulation->result  <=  Top5Population[i]->result ) 
			memcpy(BestPopulation,Top5Population[i],sizeof(plt));
	}	
	return BestPopulation;	
}
